package com.cheung.leetcode.str;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.str
 * @Author :  cheungming
 * @CreateTime : 2024-05-07 08:58:20
 * @Description : 反转单词
 * @Version : 1.0
 */
public class Code151Test {

    public String reverseWords(String s) {

        String[] strings = Arrays.stream(s.trim().split(" "))
                .filter(o -> !"".equals(o)).toArray(String[]::new);

        for (int i = 0, j = strings.length - 1; i < strings.length / 2; i++, j--) {
            String temp = strings[i];
            strings[i] = strings[j];
            strings[j] = temp;
        }

        return String.join(" ", strings);
    }


    @Test
    public void test1() {
        String s = "the sky is blue";
        String r = "blue is sky the";
        assert r.equals(reverseWords(s));
    }


    @Test
    public void test2() {
        String s = "  hello world  ";
        String r = "world hello";
        assert r.equals(reverseWords(s));
    }


    @Test
    public void test3() {
        String s = "a good   example";
        String r = "example good a";
        assert r.equals(reverseWords(s));
    }

}
